package cn.edu.zjx.controller;

import com.management.entity.User;
import com.management.service.DepartmentService;
import com.management.service.RoleService;
import com.management.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private RoleService roleService;
    
    @Autowired
    private DepartmentService departmentService;
    
    @GetMapping("/list")
    @ResponseBody
    public List<User> list() {
        try {
            List<User> users = userService.getAllUsers();
            if (users == null) {
                logger.error("获取用户列表失败：结果为空");
                return new ArrayList<>();
            }
            return users;
        } catch (Exception e) {
            logger.error("获取用户列表失败", e);
            throw new RuntimeException("获取用户列表失败：" + e.getMessage());
        }
    }
    
    @GetMapping("/add")
    public String add(Model model) {
        try {
            model.addAttribute("roles", roleService.getAllRoles());
            model.addAttribute("departments", departmentService.getAllDepartments());
            return "user/add";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", "加载添加页面失败");
            return "error";
        }
    }
    
    @PostMapping("/add")
    @ResponseBody
    public String add(@RequestBody User user) {
        return userService.addUser(user) ? "success" : "error";
    }
    
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable Integer id, Model model) {
        try {
            model.addAttribute("user", userService.getUserById(id));
            model.addAttribute("roles", roleService.getAllRoles());
            model.addAttribute("departments", departmentService.getAllDepartments());
            return "user/edit";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", "加载编辑页面失败");
            return "error";
        }
    }
    
    @PostMapping("/edit")
    @ResponseBody
    public String edit(@RequestBody User user) {
        return userService.updateUser(user) ? "success" : "error";
    }
    
    @PostMapping("/delete/{id}")
    @ResponseBody
    public String delete(@PathVariable Integer id) {
        return userService.deleteUser(id) ? "success" : "error";
    }
    
    @GetMapping("/search")
    @ResponseBody
    public List<User> search(String keyword) {
        try {
            return userService.searchUsers(keyword);
        } catch (Exception e) {
            logger.error("搜索用户失败", e);
            return new ArrayList<>();
        }
    }
    
    @GetMapping("/test")
    @ResponseBody
    public String test() {
        logger.info("访问 /user/test 接口");
        try {
            List<User> users = userService.getAllUsers();
            logger.info("成功获取到 {} 个用户", users.size());
            return "Found " + users.size() + " users";
        } catch (Exception e) {
            logger.error("测试接口发生错误", e);
            return "Error: " + e.getMessage();
        }
    }
} 